<template>
  <el-container>
    <el-header>
      <div class='logo-left'>
        <img src='../assets/heima.png' alt=''>
        <span>电商管理系统</span>
      </div>
      <el-button type='info' size='small' @click='logout'>退出登录</el-button>
    </el-header>
    <el-container style='overflow: hidden'>
      <el-aside :width="isAide?'64px':'200px'">
        <div class='aside-top' @click='toggleAside'>|||</div>
        <el-menu
          :router='true'
          background-color='#333744'
          text-color='#fff'
          :default-active="'/'+menuHigh"
          active-text-color='#2b6ee2' unique-opened :collapse='isAide' :collapse-transition='false'>
          <el-submenu :index='String(item.id)' v-for='item in menu' :key='item.id'>
            <template slot='title'>
              <i :class='menuIcon[item.id]'></i>
              <span>{{ item.authName }}</span>
            </template>
            <el-menu-item :index="'/'+items.path" v-for='items in item.children' :key='items.id'
                          @click='saveIndex(items.path)'>
              <i class='el-icon-menu'></i>
              <span>{{ items.authName }}</span>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <el-main>
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
  name: 'Home',
  data() {
    return {
      menu: [],
      menuIcon: {
        125: 'el-icon-s-custom',
        103: 'el-icon-s-marketing',
        101: 'el-icon-s-comment',
        102: 'el-icon-s-promotion',
        145: 'el-icon-s-platform'
      },
      isAide: false,
      menuHigh: ''
    }
  },
  created() {
    this.getMenuList()
    this.menuHigh = window.sessionStorage.getItem('path')
  },
  methods: {
    logout() {
      window.sessionStorage.removeItem('token')
      this.$router.push('/login')
    },
    async getMenuList() {
      const { data: res } = await this.$http.get('menus')
      if (res.meta.status !== 200) {
        return this.$msg({ message: '获取菜单列表失败', duration: 1500 })
      }
      this.menu = res.data
    },
    // 折叠侧边栏
    toggleAside() {
      this.isAide = !this.isAide
    },
    // 保存二级菜单中的索引
    saveIndex(path) {
      window.sessionStorage.setItem('path', path)
      this.menuHigh = path
    }
  }
}
</script>

<style lang='less' scoped>

.el-container {
  height: 100%;

  .el-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    background: #373d41;
    padding: 0 10px;

    .logo-left {
      display: flex;
      align-items: center;
      height: 100%;

      span {
        font-size: 20px;
        color: #FFFFFF;
        margin-left: 15px;
      }
    }

    .el-button {
      margin-right: 5px;
    }
  }

  .el-aside {
    background: #333744;
  }

  .el-main {
    background: #eaedf1;
  }
}

.el-menu {
  border-right: none;
}

.aside-top {
  width: 100%;
  height: 30px;
  line-height: 30px;
  text-align: center;
  font-size: 12px;
  color: #FFFFFF;
  border-bottom: 1px solid #999999;
  letter-spacing: 4px;
  cursor: pointer
}
</style>
